const fs = require('fs')
const path = require('path')
const query = require('../db/query')
//写入地址
const dbPath = path.join('/Users/HUAWEI/Desktop/study-99/zuoye1001/app/db', '/userdb.json')
//增加
const addUserService = async (params) => {
    // console.log(params, 'params')
    // console.log(fs,'fs');
    //读取数据
    let mydata = await JSON.parse(fs.readFileSync(dbPath), 'utf-8')
    //拼接
    mydata.push(params)
    //写入
    await fs.writeFileSync(dbPath, JSON.stringify(mydata), 'utf-8')
    return true
}
//查看
const getUserService = async (params) => {
    //读取所有的数据
    let mydata = await JSON.parse(fs.readFileSync(dbPath), 'utf-8')
    // console.log(mydata)
    return mydata
}

//修改
const updataUserService = async (params) => {
    console.log(params,'params')
    //读取所有的数据
    let mydata = await JSON.parse(fs.readFileSync(dbPath), 'utf-8')
    //循环所有数据
    await mydata.forEach(item => {
        //如果id是要修改的数据
        if (item.id == params) {
            item.username = params.username
        }
    })
    await fs.writeFileSync(dbPath, JSON.stringify(mydata), 'utf-8')
    return true
}

//删除
const delUserService = async (params) => {
    let myData = await JSON.parse(fs.readFileSync(dbPath, 'utf-8'))
    //设置空数组
    let arr = []
    //循环所有数据
    await myData.forEach(item => {
        //如果id不是要删除的数据
        if (item.id != params) {
            //数据重新写入
            arr.push(item)
        }
    })
    await fs.writeFileSync(dbPath, JSON.stringify(arr), 'utf-8')
    return true
}


// mysql
//查看数据
const getList = async (params) => {
    // 查看语句
    const sql = `select*from users`
    const result = await query(sql)
    console.log(result)
    return result
}

//增加数据
const addList = async (params) => {
    // console.log(params,'-------')
    // 插入语句
    const sql = `insert into users(username,pwd,age,phone,nickname) values('${params.username}','${params.pwd}','${params.age}','${params.phone}','${params.nickname}')`
    const result = await query(sql)
    console.log(result, '11111')
    // 1：插入成功
    if (result.affectedRows == 1) {
        return true
    } else {
        return false
    }
}

//修改数据
const updataList = async (params) => {
    //修改语句
    const sql = `update users set age='${params.age}',phone='${params.phone}' where id='${params.id}'`
    const result = await query(sql)
    console.log(result, '22222')
    //1:修改成功
    if (result.affectedRows == 1) {
        return true
    } else {
        return false
    }
}


//删除数据
const delList = async (params) => {
    //删除语句
    const sql = `delete from users where id='${params.id}'`
    const result = await query(sql)
    console.log(result, '33333')
    //1:删除成功
    if (result.affectedRows == 1) {
        return true
    } else {
        return false
    }
}
//注册
const registryUsersService = async(params)=>{
    const {username,pwd,age,phone,nickname} = params
    // console.log(username,pwd,'pwd');
    //查找有没有用户名
    let sql = `select*from users where username='${username}'`
    const result = await query(sql)
    // console.log(result,'result222222222');
    //判断有并且长度相同
    if(result&&result.length){
        return result
    }else {//没有查找到
        //插入语句
        let sql=`insert into users(username,pwd,age,phone,nickname) values('${username}','${pwd}','${age}','${phone}','${nickname}')`
        const result = await query(sql)
        // console.log(result, '55555')
        //1：数据添加成功
        if (result.affectedRows == 1) {
            return true
        }else{
            return false
        }
    }
}


//登录
const loginUsersService = async(params)=>{
    console.log(params,'121212121212');
    const {username,pwd} = params
    //查找用户名
    let sql=`select username from users where username='${username}'`
    let result = await query(sql)
    // console.log(result, '6666')
    //有用户的名称
    if (result.length>0) {
        //查找到用户名密码
        let sql=`select username from users where username='${username}' and pwd='${pwd}'`
        let result = await query(sql)
        // console.log(result);
        //如果有
        if(result.length>0){
            return{
                status:true,
                message:'用户密码正确',
                data:result[0]
            } 
        }else{
            return{
                status:false,
                message:'密码不正确'
            } 
        }

    }else{
        return{
            status:false,
            message:'用户不存在'
        } 
    }
}

module.exports = {
    addUserService,
    delUserService,
    getUserService,
    updataUserService,
    getList,
    addList,
    updataList,
    delList,

    registryUsersService,
    loginUsersService
    
}
